<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Validator;
class Admin extends Model
{	
	protected $fillable=['username','password','nickname','email'];
	protected $dateFormat='U';
	protected $dates=['created_at','updated_at','deleted_at'];

    //登录
    public function login($data){
    	$rule=[
    		'username'=>'bail|required',
    		'password'=>'required'
    	];
    	$msg=[
    		'username.required'=>'用户名不能为空',
    		'password.required'=>'密码不能为空'
    	];
    	$validator =Validator::make($data,$rule,$msg);
    	if($validator->fails()){
    		return $validator->errors()->first();
    	}
    	$result=$this->where($data)->first();
    	if($result){
    		if($result['status'] != 1){
    			return '账户被禁用';
    		}
    		$sessionData=[
    			'id'=>$result['id'],
    			'nickname'=>$result['nickname'],
				'is_super'=>$result['is_super'],
				'email'=>$result['email'],
    		];
    		session(['admin'=>$sessionData]);
    		return 1;
    	}else{
    		return '用户名或密码错误';
    	}
    }
    public function register($data){
    	$rule=[
    		'username'=>'bail|required|unique:admins',
    		'password'=>'required',
    		'conpass'=>'required|same:password',
    		'nickname'=>'required',
    		'email'=>'required|email|unique:admins'
    	];
    	$msg=[
    		'username.required'=>'用户名不能为空',
    		'username.unique'=>'用户名已存在',
    		'password.required'=>'密码不能为空',
    		'conpass.required'=>'确认密码不能为空',
    		'conpass.same|password'=>'两次密码不相同',
    		'nickname.required'=>'昵称不能为空',
    		'email.required'=>'邮箱不能为空',
    		'email.email'=>'邮箱格式不正确',
    		'email.unique'=>'邮箱已注册',
    	];
    	$validator =Validator::make($data,$rule,$msg);
    	if($validator->fails()){
    		return $validator->errors()->first();
    	}
    	$result=$this->create($data);
    	if($result){
    		return 1;
    	}else{
    		return '注册失败，请重试';
    	}

    }
    public function getCode($data){
    	$rule=[
    		'email'=>'bail|required|email'
    	];
    	$msg=[
    		'email.required'=>'邮箱不能为空',
    		'email.email'=>'邮箱格式不正确',
    	];
    	$validator =Validator::make($data,$rule,$msg);
    	if($validator->fails()){
    		return $validator->errors()->first();
    	}
    	$result=$this->where('email',$data['email'])->first();
    	if($result){
    		$code=mt_rand(100000,999999);
    		session(['code'=>$code]);
    		$content="您的验证码为：".$code;
    		$sendResult=mailto($data['email'],'重置密码验证码——梦涛刘',$content);
    		if($sendResult){
    		return 1;
    		}else{
    			return '验证码发送失败！1';
    		}
    	}else{
    		return '此邮箱尚未注册！';
    	}
    }
    public function rest($data){
    	$rule=[
    		'code'=>'bail|required',
    		'newpassword'=>'required',
    		'newconpass'=>'required|same:newpassword',
    		
    	];
    	$msg=[
    		'newpassword.required'=>'密码不能为空',
    		'newconpass.required'=>'确认密码不能为空',
    		'newconpass.same'=>'两次密码不相同',
    		'code.required'=>'验证码不能为空'
    	];
    	$validator =Validator::make($data,$rule,$msg);
    	if($validator->fails()){
    		return $validator->errors()->first();
    	}
    	if($data['code'] != session('code')){
    		return '验证码不正确！';
    	}
    	$adminInfo=$this->where('email',$data['email'])->first();
    	$adminInfo->password=$data['newpassword'];
    	$result=$adminInfo->save();
    	if($result){
    		return 1;
    	}else{
    		return '重置失败，请重试!';
    	}
    }
}
